package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.google.gson.internal.$Gson$Types;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
<<<<<<< HEAD
 * @author Wang + zhang
 * @version 1.0.0
 * @createTime 2020年08月25日
 * @Description: 权限SERVICE实现类
 */
@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Permission> findAll() {
        List<Permission> permissionList = permissionDao.findAll();
        return permissionList;
    }

    /**
     * 分页查询
     * @param param
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean param) {
        log.info("[分页查询]参数:{}",param);
        //设置参数
        PageHelper.startPage(param.getCurrentPage(),param.getPageSize());
        //分页查询数据库
        Page<Permission> page = permissionDao.selectByCondition(param.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 添加
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        log.info("[权限管理-添加权限]data:{}",permission);
        permissionDao.insert(permission);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id) {
        log.info("[权限管理-删除权限]id:{}",id);
        Long count = permissionDao.countRoleByPermissionId(id);
        if (count > 0){
            throw new BusinessRuntimeException("当前权限有数据,不能删除");
        }
        permissionDao.deleteById(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.selectById(id);
    }

    /**
     * 编辑
     * @param permission
     */
    @Override
    public void edit(Permission permission) {
        log.info("[权限管理-编辑]data:{}",permission);
        permissionDao.update(permission);
    }
}
